package cn.yunhe.util;

import cn.yunhe.model.Brand;

import java.util.Random;

public class BrandUtil {
    public static Random random = new Random();
    public static String[] brandNames = {
            "刷子","剪刀","叉子",
            "手套","水桶","火",
            "玉米","球","瓶子",
            "白菜","稻草","肉腿",
            "胡萝卜","苹果","苹果",
            "铃铛","青草"
    };
    /**
     * 每次调⽤获取⼀个牌的名称
     * @return
     */
    public static String getBrandName (){
        int randomIndex = random.nextInt(brandNames.length);
        return brandNames[randomIndex];
    }
    /**
     * 创建随机牌的数组
     * @param capacity
     * @return
     */
    public static Brand[] buildBrands(int capacity){
        Brand[] brands = new Brand[capacity];//数组的容量和图层的容量相等
        for (int i = 0; i < brands.length; i+=2) {
            String randomBrandName = getBrandName(); //获得排名
            Brand brand1 = new Brand(randomBrandName);//三张牌相同的牌
            Brand brand2 = new Brand(randomBrandName);
//            Brand brand3 = new Brand(randomBrandName);
            brands[i] = brand1;
            brands[i+1] = brand2;
//            brands[i+2] = brand3;
        }

        for (int i = 0; i < brands.length; i ++) {
            Brand brandA = brands[i];//当前位置A
            //交换位置B的索引
            int randomIndex = random.nextInt(brands.length);
            Brand brandB = brands[randomIndex];
            Brand temp = brandA;
            brands[i] = brandB;
            brands[randomIndex] = temp;
        }
        
        return brands;
    }
}